<?php

namespace app\admin\service;

use app\common\model\PromotionGroup;
use think\facade\Config;

/**
 * 渠道数据
 * Class PromotionGroupFieldService
 * @package app\admin\service
 */
class PromotionGroupFieldService
{
    private PromotionTable2Service $table2Service;

    public function __construct(PromotionTable2Service $table2Service)
    {
        $this->table2Service = $table2Service;
    }

    /**
     * 渠道分组对比数据统计
     * @param array $promotionGroupIds
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public function index(array $promotionGroupIds, $startTime, $endTime)
    {
        $promotion = Config::get('promotion', []);

        $days = [];
        foreach ($promotion as $item) {
            $days[] = ['name' => $item['title']];
        }

        $fields = array_column($promotion, 'value');

        $list = [];

        PromotionGroup::whereIn('id', $promotionGroupIds)
            ->select()
            ->each(function (PromotionGroup $promotionGroup) use (&$list, $fields, $startTime, $endTime) {

                $list[] = [
                    'name' => $promotionGroup->groupName,
                    'datas' => $this->table2Service->indexOfGroup2($promotionGroup->id, $fields, $startTime, $endTime),
                ];
            });

        return [
            'day_count' => count($days),
            'days' => $days,
            'list' => $list,
        ];
    }

}
